<template>
	<view class="container">
		<!-- 原始密码 -->
		<view class="init-content" v-show="!isCheckInitPass">
			<text class="password-text">密码</text>
			<input 
				class="init-pass" 
				v-model="initPass" 
				placeholder-style="font-size:32upx;font-family:PingFangSC-Regular,PingFang SC;color:#BBBBBB;"
				placeholder="请输入密码" 
			>
			<text class="tips">提示：初始密码为guoke686868，请及时登录PC端修改密码</text>
			<button @click="nextChek" class="next-btn">下一步</button>
		</view>
		<!-- 新手机号 -->
		<view class="update-content" v-show="isCheckInitPass">
			<text class="phone-text">绑定新号码</text>
			<input 
				class="phone-number" 
				v-model="phoneNumber" 
				placeholder-style="font-size:32upx;font-family:PingFangSC-Regular,PingFang SC;color:#BBBBBB;"
				placeholder="请输入手机号" 
				type="number"
			>
			<text class="phone-text">短信验证码</text>
			<view class="message-content">
				<input 
					type="text" 
					v-model="message" 
					placeholder="请输入短信验证码" 
					placeholder-style="font-size:32upx;font-family:PingFangSC-Regular,PingFang SC;color:#BBBBBB;"
					class="message-input"
				>
					<button class="message-btn" @click="getSms">{{liked ? '获取验证码' : count + '秒后重发'}}</button>
			</view>
			<button @click="checkUpdate" class="check-btn">确认</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				initPass: '',
				isCheckInitPass: false,
				phoneNumber: '',
				message: '',
				countdown: '获取验证码',
				count: 60,
				liked: true,
			}
		},
		methods: {
			nextChek() {
				this.$request.post({
					url: 'user/checkingPasswd',
					data: {passwd: this.initPass},
				}).then(res=>{
					this.isCheckInitPass = true;
				},res=>{
				});
			},
			getSms() {
				if(!this.liked)return;
				let that = this;
				if (this.phoneNumber === '') {
					uni.showToast({
						title: '请输入手机号',
						icon:'none'
					});
					return;
				}
				 this.$request.post({
				 	url: 'message/sendMessage',
				 	data: {telephoneNumber: this.phoneNumber, reset: 'ph'},
				 }).then(res=>{
					that.countDown();
				 },res=>{
				 });
			},
			countDown() {
				const that = this;
			 if (this.count === 1) {
				 this.count = 60;
				 this.liked = true;
				} else {
					this.count = this.count - 1;
					this.liked = false;
					setTimeout(that.countDown, 1000);
				}
			},
			checkUpdate() {
				const userInfo = uni.getStorageSync('userInfo');
				if (this.phoneNumber === '') {
					uni.showToast({
						title: '请输入手机号',
						icon:'none'
					});
					return;
				}
				if (this.message === '') {
					uni.showToast({
						title: '请输入验证码',
						icon:'none'
					});
					return;
				}
				this.$request.post({
					url: 'user/updateUserinfo',
					data: {phoneNumber: this.phoneNumber, message: this.message, userId: userInfo.userId},
				}).then(res=>{
					uni.removeStorageSync('userInfo');
					uni.switchTab({
						url: '/pages/myinfo/myinfo'
					});
				},res=>{
				});
			}
		}
	}
</script>

<style scoped lang="less">
	.container {
		background: #F5F6F9;
		padding: 50upx 30upx 0 30upx;
		height: 100vh;
		.init-content {
			.password-text {
				color: #333333;
				font-size: 28upx;
			}
			.init-pass {
				width: auto;
				height: 90upx;
				background: #FFFFFF;
				padding-left: 22upx;
				margin: 22upx 0 28upx 0;
				border: 1px solid #EDEDED;
			}
			.tips {
				font-size: 24upx;
				color: #424242;
			}
			.next-btn {
				color: #ffffff;
				font-size: 36upx;
				background:linear-gradient(90deg,rgba(70,144,255,1) 0%,rgba(42,125,250,1) 100%);
				border-radius: 44upx;
				margin-top: 76upx;
			}
		}
		.update-content {
			.phone-text {
				color: #333333;
				font-size: 28upx;
			}
			.phone-number {
				width: auto;
				height: 90upx;
				background: #FFFFFF;
				padding-left: 22upx;
				margin: 22upx 0 28upx 0;
				border: 1px solid #EDEDED;
			}
			.message-content {
				display: flex;
				align-items: center;
				.message-input {
					flex: 1;
					// width: auto;
					// width: 400upx;
					height: 90upx;
					background: #FFFFFF;
					padding-left: 22upx;
					margin: 22upx 0 28upx 0;
					border: 1px solid #EDEDED;
					margin-right: 20upx;
				}
				.message-btn {
					height: 90upx;
					line-height: 90upx;
					border-radius: 10upx;
					font-size: 32upx;
					background:linear-gradient(90deg,rgba(70,144,255,1) 0%,rgba(42,125,250,1) 100%);
					color: #ffffff;
					margin-top: -2upx;
				}
			}
			.check-btn {
				color: #ffffff;
				font-size: 36upx;
				background:linear-gradient(90deg,rgba(70,144,255,1) 0%,rgba(42,125,250,1) 100%);
				border-radius: 44upx;
				margin-top: 76upx;
			}
		}
	}
</style>
